<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('product.teacher.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('teacher.name')" prop="teacher_id">
            <el-select
              v-model="dataForm.teacher_id"
              :placeholder="i18n('common.please_select') + i18n('teacher.name')"
              multiple collapse-tags clearable>
              <el-option v-for="(v, k) in teacherList" :label="v.name" :key="k" :value="v.id"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('common.status')" prop="status">
            <el-switch
              v-model="dataForm.status"
              active-value="1"
              :active-text="i18n('common.enable')"
              inactive-value="2"
              :inactive-text="i18n('common.disable')">
            </el-switch>
          </el-form-item>

          <el-form-item>
            <el-button
              v-if="isAuth('module:business:product:teacher:handle')"
              type="primary"
              @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>

            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="product-teacher-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/product/teacher')

  const teacherList = ref([])

  const dataForm = reactive({
    id: 0,
    product_id: 0,
    teacher_id: '',
    sort: 0,
    status: '1',
  })

  const dataRule = reactive({
    teacher_id: [{
      required: true,
      message: i18n('product.teacher.rules.teacher_id.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.product_id = data.data.product_id
            dataForm.teacher_id = data.data.teacher_id
            dataForm.sort = data.data.sort
            dataForm.status = data.data.status.value + ''
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate((valid) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/handle`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            product_id: dataForm.product_id,
            teacher_id: dataForm.teacher_id,
            sort: dataForm.sort,
            status: dataForm.status,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  const loadTeacherList = () => {
    $http({
      url: $http.adornUrl('/business/product/teacher/select'),
      method: 'get',
      params: $http.adornParams({
        'product_id': dataForm.product_id
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        teacherList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    dataForm.product_id = query.product_id

    init(formRef)

    loadTeacherList()
  })
</script>
